<template>
  <div>

        <div class="card" style="margin-bottom: 5px;">
          <el-input v-model="data.patientName" style="width: 300px; margin-right: 10px" placeholder="请输入患者姓名查询"></el-input>
          <el-button type="primary" @click="load">查询</el-button>
          <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
        </div>

    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" stripe>
        <el-table-column label="患者ID" prop="patientId"></el-table-column>
        <el-table-column label="患者名" prop="patientName"></el-table-column>
        <el-table-column label="医生ID" prop="doctorId"></el-table-column>
        <el-table-column label="医生名" prop="doctorName"></el-table-column>
        <el-table-column label="预约挂号日期时间" prop="visitDateTime" width="140"></el-table-column>
        <el-table-column label="创建时间" prop="createdTime"></el-table-column>
        <el-table-column label="预约状态" prop="status" width="140">
          <template v-slot="scope">
            <el-tag
                :type="getStatusTagType(scope.row.status)"
                disable-transitions
            >
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="285">
          <template #default="scope">
            <el-button type="success" plain @click="handleAccept(scope.row)" v-if="scope.row.status === '等待处理'">接收预约</el-button>
            <el-button type="warning" plain @click="handleReject(scope.row)" v-if="scope.row.status === '等待处理'">拒绝预约</el-button>
            <el-button type="primary" plain @click="handleCall(scope.row)" v-if="scope.row.status === '挂号成功，等待叫号'">叫 号</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card">
      <el-pagination background layout="prev, pager, next" v-model:page-size="data.pageSize"
                     v-model:current-page="data.pageNum" :total="data.total"/>
    </div>

  </div>
</template>

<script setup>
import request from "@/utils/request";
import {reactive} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  formVisible: false,
  form: {},
  tableData: [],
  patientName: null,
  user: JSON.parse(localStorage.getItem('system-user') || '{}')
})

// 分页查询
const load = () => {
  request.get('/RegistrationForm/selectPageInDoctor', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      patientName: data.patientName,
      doctorId: data.user.id
    }
  }).then(res => {
    data.tableData = res.data?.list
    data.total = res.data?.total
  })
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗?', '删除确认', {type: 'warning'}).then(res => {
    request.delete('/RegistrationForm/delete/' + id).then(res => {
      if (res.code === '200') {
        load()
        ElMessage.success('操作成功')
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(err => {
  })
}

// 重置
const reset = () => {
  data.patientName = null
  load()
}

// 接收预约
const handleAccept = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.form.status = "挂号成功，等待叫号"
  request.put('/RegistrationForm/update', data.form).then(res => {
    if (res.code === '200') {
      load()
      ElMessage.success('操作成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 拒绝预约
const handleReject = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.form.status = "预约失败"
  request.put('/RegistrationForm/update', data.form).then(res => {
    if (res.code === '200') {
      load()
      ElMessage.success('操作成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
}
// 叫号
const handleCall = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.form.status = "已叫号"
  request.put('/RegistrationForm/update', data.form).then(res => {
    if (res.code === '200') {
      load()
      ElMessage.success('叫号成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
  addNewPatientVisit()
}
const addNewPatientVisit = () => {
  let params = {
    patientId: data.form.patientId,
    doctorId: data.form.doctorId,
    registrationId: data.form.id
  }
  request.post('/PatientVisit/add', params).then(res => {
    if (res.code === '200') {
      ElMessage.success('操作成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
}
// 根据挂号状态返回对应的标签类型
const getStatusTagType = (status) => {
  switch (status) {
    case '挂号成功，等待叫号':
      return 'success';
    case '预约失败':
      return 'danger';
    case '已叫号':
      return 'info';
    case '等待处理':
      return 'warning';
    default:
      return '';
  }
};

load()

</script>